package com.emip.service.impl;

import java.sql.ResultSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.MenuDefBean;
import com.emip.bean.RoleMenuBean;
import com.emip.config.CommonOperation;
import com.emip.dao.MenuDefDAO;
import com.emip.dao.RoleMenuDAO;
import com.emip.service.MenuDefService;

@Service("menuDefService")
public class MenuDefServiceImpl implements MenuDefService{

	@Autowired
	MenuDefDAO menuDefDAO;
	@Autowired
	RoleMenuDAO roleMenuDAO;
	
	public List<MenuDefBean> getMenuList() {
		
		List<MenuDefBean> l1 = this.menuDefDAO.getByLevel("1");		
		List<MenuDefBean> l2 = this.menuDefDAO.getByLevel("2");
		
		for(int i=0;i<l2.size();i++)
		{
			MenuDefBean child = l2.get(i);
			for(int j=0;j<l1.size();j++)
			{
				MenuDefBean bean = l1.get(j);
				if(bean.getId().equals(child.getParentID()))
				{
					bean.getChildren().add(child);
					break;
				}
			}
		}
		
		return l1;
	}

	public List<MenuDefBean> getMenuListByRole(int roleId) {
		List<MenuDefBean> l1 = this.menuDefDAO.getByRoleLevel("1", roleId);		
		List<MenuDefBean> l2 = this.menuDefDAO.getByRoleLevel("2", roleId);
		for(int i=0;i<l2.size();i++)
		{
			MenuDefBean child = l2.get(i);
			for(int j=0;j<l1.size();j++)
			{
				MenuDefBean bean = l1.get(j);
				if(bean.getId().equals(child.getParentID()))
				{
					bean.getChildren().add(child);
					break;
				}
			}
		}
		return l1;
	}
}
